var app = getApp()
Page({
  data: {
    peoplePrice:0,
    goodsList:[],
    laborCostTime:'0',
    laborItem:{
      id:0,
      price:0
    },
    modalPrice:'',
    modalIndex:'',
    isModal:false,
    settlementPrice:'',
    routeControl:true,
    accountId:'',
    allChoose:false,
    isEdit:false,
    isOwner:false,
    noData:false,
    totalDeliberation:0,
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getSettlement()
      if (res.data.role=='SHOP_OWNER') {
        this.setData({
          isOwner:true
        })
      }
    })
    if (options.type==0) {
      this.setData({
        addPeopleShow:true
      })
    }else{
      this.setData({
        addPeopleShow:true,
        routeControl:false
      })
    }
    // this.totalPrice()
  },
  onShow:function(){
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId,
        accountId:res.data.id
      })
      this.getSettlement()
      if (res.data.role=='SHOP_OWNER') {
        this.setData({
          isOwner:true
        })
      }
    })
    // this.totalPrice()
  },
  bindtapNumber(e){
    let count = parseInt(e.detail.value)
    let i = e.currentTarget.dataset.index
    let j = e.currentTarget.dataset.jndex
    let id = e.currentTarget.dataset.id
    let data = {
      id,
      count
    }
    app.request('wx_app/stock_cart/updateCount',data,'GEt').then(res=>{
      console.log(res)
      if(res.code==1){

      }else{
        app.errorMsg(res.message)
      }
    })
    this.setData({
      ['goodsList['+i+'].carts['+j+'].count']:count
    })
    const good = this.data.goodsList[i].carts[j]
    if (good.deliberation==1) {
      if (good.number3!=null) { //三个价格
        if (count<good.number1) { //比第一个价格少
          console.log(count+'三个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) { //在第一个价格和第二个价格之间
          console.log(count+'三个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2&&count<good.number3) { //在第二个价格和第三个价格之间
          console.log(count+'三个价格 在第二个价格和第三个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }else if (count>=good.number3) { //大于第三个价格
          console.log(count+'三个价格 大于第三个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice3
          })
        }
      }else if (good.number2!=null) { //两个价格
        if (count<good.number1) {
          console.log(count+'两个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) {
          console.log(count+'两个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2) {
          console.log(count+'两个价格 大于第二个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }
      }else{ //一个价格
        if (count<good.number1) {
          console.log(count+'一个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1) {
          console.log(count+'一个价格 大于第一个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }
      }
    }
    this.getPirce()
  },
  add(e){
    let count = e.currentTarget.dataset.count
    let i = e.currentTarget.dataset.index
    let j = e.currentTarget.dataset.jndex
    let id = e.currentTarget.dataset.id
    console.log(count,i)
    count++
    let data = {
      id,
      count
    }
    app.request('wx_app/stock_cart/updateCount',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {

      }else{
        app.errorMsg(res.message)
      }
    })
    this.setData({
      ['goodsList['+i+'].carts['+j+'].count']:count
    })
    const good = this.data.goodsList[i].carts[j]
    console.log(good)
    if (good.deliberation==1) {
      if (good.number3!=null) { //三个价格
        if (count<good.number1) { //比第一个价格少
          console.log(count+'三个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) { //在第一个价格和第二个价格之间
          console.log(count+'三个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2&&count<good.number3) { //在第二个价格和第三个价格之间
          console.log(count+'三个价格 在第二个价格和第三个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }else if (count>=good.number3) { //大于第三个价格
          console.log(count+'三个价格 大于第三个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice3
          })
        }
      }else if (good.number2!=null) { //两个价格
        if (count<good.number1) {
          console.log(count+'两个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) {
          console.log(count+'两个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2) {
          console.log(count+'两个价格 大于第二个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }
      }else{ //一个价格
        if (count<good.number1) {
          console.log(count+'一个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1) {
          console.log(count+'一个价格 大于第一个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }
      }
    }
    this.getPirce()
    // this.totalPrice()
  },
  sub(e){
    console.log('sub')
    let count = e.currentTarget.dataset.count
    let i = e.currentTarget.dataset.index
    let j = e.currentTarget.dataset.jndex
    let id = e.currentTarget.dataset.id
    count--
    if (count>0) {
      let data = {
        id,
        count
      }
      app.request('wx_app/stock_cart/updateCount',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {

        }else{
          app.errorMsg(res.message)
        }
      })
      this.setData({
        ['goodsList['+i+'].carts['+j+'].count']:count
      })
    }
    const good = this.data.goodsList[i].carts[j]
    console.log(good)
    if (good.deliberation==1) {
      if (good.number3!=null) { //三个价格
        if (count<good.number1) { //比第一个价格少
          console.log(count+'三个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) { //在第一个价格和第二个价格之间
          console.log(count+'三个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2&&count<good.number3) { //在第二个价格和第三个价格之间
          console.log(count+'三个价格 在第二个价格和第三个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }else if (count>=good.number3) { //大于第三个价格
          console.log(count+'三个价格 大于第三个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice3
          })
        }
      }else if (good.number2!=null) { //两个价格
        if (count<good.number1) {
          console.log(count+'两个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1&&count<good.number2) {
          console.log(count+'两个价格 在第一个价格和第二个价格之间')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number2) {
          console.log(count+'两个价格 大于第二个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice2
          })
        }
      }else{ //一个价格
        if (count<good.number1) {
          console.log(count+'一个价格 比第一个价格少')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }else if (count>=good.number1) {
          console.log(count+'一个价格 大于第一个价格')
          this.setData({
            ['goodsList['+i+'].carts['+j+'].price']:good.setPrice1
          })
        }
      }
    }
    // this.totalPrice()
    this.getPirce()
  },
  totalPrice(){
    let [total,length,mark] = [0,0,0]
    this.data.goodsList.forEach((item,i)=>{
      item.carts.forEach((data,j)=>{
        if (data.isChecked==1) {
          mark += data.deliberation
          length++
          let count = data.count*data.price
          total+=count
        }
      })
    })
    if (mark<length) {
      this.setData({
        totalDeliberation:0
      })
    }else{
      this.setData({
        totalDeliberation:1
      })
    }
    this.setData({
      settlementPrice:Math.floor(total * 100) / 100
    })
  },
  bindToConfirmOrder(){
   wx.navigateTo({
    url:'confirmOrder'
  })
 },
 bindToOrderDetail(e){
    let index = e.currentTarget.dataset.index
    let stockCarts = []
    let carts = []
    let applyType = ''
    //是否是扮马
    if (this.data.goodsList[index].companyName==='扳马') {
      applyType = 0
    }else{
      applyType = 1
    }
    if (this.data.goodsList[index].check) {
      stockCarts[0] = this.data.goodsList[index]
    }else{

      this.data.goodsList[index].carts.forEach(item=>{
        if (item.isChecked==1) {
          carts.push(item)
        }
      })
      stockCarts[0] = this.data.goodsList[index]
      delete stockCarts[0].carts
      stockCarts[0].carts = carts
      console.log(stockCarts)
      console.log(carts)
    }
      console.log(stockCarts)
      let data = {
        stockCarts:JSON.stringify(stockCarts),
        agentId:this.data.agentId,
        accountId:this.data.accountId
      }
      console.log(data)
      app.request('wx_app/agent_order/saveAgentOrderByCart',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          let orderId = res.data[0].id
          wx.navigateTo({
            url:'confirmOrder?orderId=' + orderId + '&applyType=' + applyType + '&type=0' //0批发1拼单
          })
        }else{
          app.errorMsg(res.message)
          this.getSettlement()
        }
      })


    },
    addLabor(){
      wx.navigateTo({
        url:'laborCost'
      })
    },
    bindSettlement(){

    },
    bindEditPrice(e){
      this.setData({
        modalPrice:e.currentTarget.dataset.price,
        modalIndex:e.currentTarget.dataset.index,
        isModal:true
      })
    },
    cancelModal(){
      this.setData({
        isModal:false
      })
    },
    confirmModal(){
      this.setData({
        isModal:false
      })
    },
    changePrice(e){
      console.log(e.detail.value)
      let price = parseInt(e.detail.value)
      let i = this.data.modalIndex
      this.setData({
        ['goodsList['+i+'].price']:price
      })
    },
    chooseItem(e){
      console.log(321)
      let i = e.currentTarget.dataset.i
      let j = e.currentTarget.dataset.j
      if (this.data.goodsList[i].carts[j].isChecked==0) {
        this.setData({
          ['goodsList['+i+'].carts['+j+'].isChecked']:1
        })
      }else{
        this.setData({
          ['goodsList['+i+'].carts['+j+'].isChecked']:0
        })
      }
    //厂商全选/反选
    let num = 0
    let cart = this.data.goodsList[i].carts
    let chcek = 0
    cart.forEach(item=>{
      num+=item.isChecked
    })
    if (num==cart.length) {
      chcek = 1
    }else{
      chcek = 0
    }
    if (chcek===1) {
      this.setData({
        ['goodsList['+i+'].check']:true
      })
    }else{
      this.setData({
        ['goodsList['+i+'].check']:false
      })
    }
    this.allChoose()
    // this.totalPrice()
  },
  cartsAllChoose(e){
    //厂商全选/反选
    let i = e.currentTarget.dataset.i
    let cart = this.data.goodsList[i]
    console.log(cart)
    if (cart.check) {
      this.data.goodsList[i].carts.forEach((item,index)=>{
        this.setData({
          ['goodsList['+i+'].carts['+index+'].isChecked']:0
        })
      })
      this.setData({
        ['goodsList['+i+'].check']:false
      })
    }else{
      this.data.goodsList[i].carts.forEach((item,index)=>{
        this.setData({
          ['goodsList['+i+'].carts['+index+'].isChecked']:1
        })
      })
      this.setData({
        ['goodsList['+i+'].check']:true,
      })
    }
    this.allChoose()
  },
  allChoose(){
    //全选/反选
    let goodsList = this.data.goodsList
    let num = 0
    goodsList.forEach(item=>{
      if (item.check) {
        num+=1
      }else{
        num+=0
      }
    })
    if (num==goodsList.length) {
      this.setData({
        allChoose:true
      })
    }else{
      this.setData({
        allChoose:false
      })
    }
    // this.totalPrice()
  },
  bindAllChoose(){
    if (this.data.allChoose) { //反选
      this.data.goodsList.forEach((item,i)=>{
        this.setData({
          ['goodsList['+i+'].check']:false
        })
        item.carts.forEach((data,j)=>{
          this.setData({
            ['goodsList['+i+'].carts['+j+'].isChecked']:0
          })
        })
      })

      this.setData({
        allChoose:false
      })
    }else{  //全选
      this.data.goodsList.forEach((item,i)=>{
        this.setData({
          ['goodsList['+i+'].check']:true
        })
        item.carts.forEach((data,j)=>{
          this.setData({
            ['goodsList['+i+'].carts['+j+'].isChecked']:1
          })
        })
      })
      this.setData({
        allChoose:true
      })
    }
    // this.totalPrice()
  },
  //获取购物车详情
  getSettlement(){
    let data = {
      accountId:this.data.accountId,
      agentId:this.data.agentId
    }
    console.log(data)
    var that = this
    app.request('wx_app/stock_cart/queryCartListByAgentId',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        res.data.forEach(item=>{
          if (res.data.length==0) {
            this.setData({
              allChoose:false
            })
          }else{
            this.setData({
              allChoose:true
            })
          }
          item.check = true
          item.carts.forEach(data=>{
            data.isChecked = 1
            if (data.deliberation==1) {
              if (data.number3!=null) {
                if (data.count<data.number1) {
                  data.price=data.setPrice1
                }else if (data.count>=data.number1&&data.count<data.number2) {
                  data.price=data.setPrice1
                }else if (data.count>=data.number2&&data.count<data.number3) {
                  data.price=data.setPrice2
                }else if (data.count>=data.number3) {
                  data.price=data.setPrice3
                }
              }else if (data.number2!=null) {
                if (data.count<data.number1) {
                  data.price=data.setPrice1
                }else if (data.count>=data.number1&&data.count<data.number2) {
                  data.price=data.setPrice1
                }else if (data.count>=data.number2) {
                  data.price=data.setPrice2
                }
              }else{
                if (data.count<data.number1) {
                  data.price=data.setPrice1
                }else if (data.count>=data.number1) {
                  data.price=data.setPrice1
                }
              }
            }else if(data.deliberation==0){
              data.price=0
            }else{
              
            }
          })
        })
        that.setData({
          goodsList:res.data
        })
        this.getPirce()
        // that.totalPrice()
      }
    })
  },
  bindEdit(){
    this.setData({
      isEdit:true
    })
  },
  bindDelete(){
    wx.showLoading({
      title:'正在删除'
    })
    let delBox = []
    let goodsList = this.data.goodsList
    goodsList.forEach(item=>{
      item.carts.forEach(data=>{
        if (data.isChecked==1) {
          delBox.push(data.id)
        }
      })
    })
    let stockCartId = delBox.join(',')
    let data = {
      stockCartId
    }
    console.log(data)
    app.request('wx_app/stock_cart/delStockByCartId',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.getSettlement()
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
    if (this.data.goodsList.length==0) {
      this.setData({
        noData:true
      })
    }else{
      this.setData({
        noData:false
      })
    }
  },
  bindComplete(){
    this.setData({
      isEdit:false
    })

  },
  //每个厂商订单总计
  getPirce(){
    let price = 0
    let amount = 0
    this.data.goodsList.forEach((item,i)=>{
      let total = 0
      item.carts.forEach(data=>{
        price = data.count * data.price
        amount = Math.floor(price * 100) / 100
        total += parseFloat(amount)
      })
      this.setData({
        ['goodsList['+i+'].total']: total
      })

    })
  }
})
